package com.outfood.consumer.hander;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.outfood.domain.Store;
import com.outfood.service.IStoreService;
import com.outfood.service.impl.MeiTuanOperateServiceImpl;
import com.outfood.utils.TokenUtils;
import com.outfood.vo.DownInfo;
import com.rys.common.utils.DateUtils;
import com.rys.common.utils.StringUtils;
import com.tps.open.mq.model.MessageBody;
import com.tps.open.weixin.vo.XcxPayNotifyInfoVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Log4j2
@Component
public class UpNoticeComsumerHander implements  ComsumerHander{

    @Resource
    RestTemplate restTemplate;
    @Resource
    TokenUtils tokenUtils;
    @Resource
    IStoreService storeService;
    @Resource
    MeiTuanOperateServiceImpl meiTuanOperateService;
    @Override
    public void toHandle(MessageBody messageBody) {

        String body = messageBody.getBody();
        DownInfo downInfo = JSON.parseObject(body, DownInfo.class);
        Store store = storeService.get(downInfo.getStoreId());
        List<String> gzhOpenIdList = meiTuanOperateService.getGzhOpenIdList(store.getId());
        gzhOpenIdList.forEach(gzhOpenId ->{
            try {
                if(StringUtils.isBlank(gzhOpenId)){
                    return;
                }
                String storeName = store.getStoreName().length() > 20?store.getStoreName().substring(0,17)+"...":store.getStoreName();
                String time = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm",new Date());
                String path = "/pages/index/index";
                String params = "      {\n" +
                        "           \"touser\":\""+gzhOpenId+"\",\n" +
                        "           \"template_id\":\"WH0Yww2Cn2CTZb1Q6l3DkgV8jD1vAocwn-SL6TuXEQ0\",\n" +
                        "           \"miniprogram\":{\n" +
                        "             \"appid\":\"wx2c8bdffa8ed7d458\",\n" +
                        "             \"pagepath\":\""+path+"\"\n" +
                        "           },\n" +
                        "           \"data\":{\n" +
                        "\n" +
                        "                   \"thing2\":{\n" +
                        "                       \"value\":\""+storeName+"\"\n" +
                        "                   },\n" +
                        "                   \"time4\": {\n" +
                        "                       \"value\":\""+time+"\"\n" +
                        "                   }\n" +
                        "           }\n" +
                        "       }";
                JSONObject jsonObject = JSON.parseObject(params);

                ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + tokenUtils.getAccessTekn(), jsonObject, String.class);
                log.info(stringResponseEntity.getBody());
            } catch (RestClientException e) {
                log.error("发送公众号模板消息失败",e);
            }
        });

    }
}
